﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.RemoteCtrls.ActivityToolBox.Node
{
    internal class SortableObservableCollection<T> : ObservableCollection<T>
    {
        public SortableObservableCollection()
            : base()
        {
        }

        public SortableObservableCollection(List<T> l)
            : base(l)
        {
        }

        public SortableObservableCollection(IEnumerable<T> l)
            : base(l)
        {
        }

        public IOrderedEnumerable<T> OrderBy<TKey>(Func<T, TKey> keySelector)
        {
            return Items.OrderBy(keySelector);
        }

        public IOrderedEnumerable<T> OrderByDescending<TKey>(Func<T, TKey> keySelector)
        {
            return Items.OrderByDescending(keySelector);
        }

        public void Sort(IOrderedEnumerable<T> items)
        {
            InternalSort(items);
        }

        private void InternalSort(IEnumerable<T> sortedItems)
        {
            var sortedItemsList = sortedItems.ToList();

            foreach (var item in sortedItemsList)
            {
                Move(IndexOf(item), sortedItemsList.IndexOf(item));
            }
        }
    }
}
